package com.mmloo.platform.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mmloo.common.page.Pager;
import com.mmloo.common.repository.CURDRepository;
import com.mmloo.common.util.DataTable;
import com.mmloo.common.util.TimeUtil;
import com.mmloo.platform.model.ShoppingCart;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by jessy on 2015/10/21.
 */
@Service
public class BusinessService {
    @Autowired
    CURDRepository curdRepository;

    public String  generateId(String tableName) throws Exception {
        ArrayList arrayList = new ArrayList();
        arrayList.add(tableName.getBytes());
        curdRepository.incr(arrayList);
        return curdRepository.get(arrayList);
    }

    public void addCart(ShoppingCart shoppingCart) throws Exception {
        ArrayList<byte[]> hsetparames = new ArrayList<>();
        hsetparames.add(generateId(DataTable.SHOPPING_CART_ID_SQUENCE).getBytes());
        hsetparames.add(DataTable.SHOPPING_CART_DETAIL_DATA.getBytes());
        shoppingCart.setId(generateId(DataTable.SHOPPING_CART_ID_SQUENCE));
        hsetparames.add(shoppingCart.toJson().getBytes());
        curdRepository.hset(hsetparames);
    }

    public ShoppingCart findCart(String id) throws Exception {
        ArrayList<byte[]> parames = new ArrayList<>();
        parames.add(DataTable.SHOPPING_CART_DETAIL_DATA.getBytes());
        parames.add(id.getBytes());
        String result = curdRepository.hget(parames);
        ShoppingCart shoppingCart = JSON.parseObject(result, ShoppingCart.class);
        return shoppingCart;
    }

    public ShoppingCart findCartByOwner(String id) throws Exception {
        List<ShoppingCart> shoppingCarts = findAllCarts();
        for(ShoppingCart shoppingCart : shoppingCarts){
            if(shoppingCart.getId().equals(id)) {
                if(TimeUtil.getCurrentTimeInString()-shoppingCart.getBirthTime()<=60000)
                return shoppingCart;
            }
        }
        return null;
    }

    public List<ShoppingCart> findAllCarts() throws Exception {
        Map<String, String> categories = curdRepository.hscanAll(DataTable.SHOPPING_CART_DETAIL_DATA);
        Set<Map.Entry<String, String>> entrySet = categories.entrySet();
        List<ShoppingCart> list = new ArrayList<>();
        if (!entrySet.isEmpty())
            for (Map.Entry<String, String> anEntrySet : entrySet) {
                String value = anEntrySet.getValue();
                ShoppingCart goods = JSONObject.parseObject(value, ShoppingCart.class);
                //有效对象
                if (goods.getValid() != 0)
                    list.add(goods);
            }
        return list;
    }

    public void update(ShoppingCart shoppingCart) throws Exception {
        ArrayList<byte[]> parames = new ArrayList<>();
        parames.add(DataTable.SHOPPING_CART_DETAIL_DATA.getBytes());
        parames.add(shoppingCart.getId().getBytes());
        parames.add(shoppingCart.toJson().getBytes());
        curdRepository.hset(parames);
    }

    public void delete(String id) throws Exception {
        ShoppingCart shoppingCart = findCart(id);
        shoppingCart.setValid(0);
        update(shoppingCart);
    }
}
